package com.real.fact.web.application;

import java.util.List;

import org.apache.commons.lang.Validate;

import com.globant.katari.core.application.Command;

/**
 * Represents an abstraction for commands that list entities with paging
 * features. Defines a common behavior for retrieving the entities from the
 * repository and allows the subclasses to customize the filter to apply.
 * @param <T> the type being listed.
 */
public abstract class AbstractPagedListCommand<T> implements
    Command<List<T>> {

  /** Default page size. */
  private static final int PAGE_SIZE = 10;

  /** The paging configuration. It's never null.*/
  private final RealPaging paging;


  /** Creates a new AbstractPagedListCommand.
   */
  public AbstractPagedListCommand() {
    paging = new RealPaging();
    paging.setPageSize(PAGE_SIZE);
    paging.setPageNumber(0);
  }


  /**
   * Returns the paging information that will be used to paginate the results.
   * @return the paging.
   */
  public RealPaging getPaging() {
    return paging;
  }

  /**
   * Sets the page number that the user needs to see.
   * @param page the page number, must be greater or equal than 0.
   */
  public void setPage(final int page) {
    Validate.isTrue(page >= 0, "Page must be greater or equal than 0");
    paging.setPageNumber(page);
  }

  /**
   * Sets the page size.
   * @param pageSize the page size. Must be greater than 0.
   */
  public void setPageSize(final int pageSize) {
    Validate.isTrue(pageSize > 0, "Page size must be greater than 0");
    paging.setPageSize(pageSize);
  }
}
